﻿var curhashpath="";
window.onhashchange = function(){
		resolveHash();
};


$(function(){
	tabRightEven();
	bindTabEven();
	//$("<bgsound id='imm_snd_div' loop='0' src=''/>").appendTo(document.body);
	//window.setTimeout(receiveImmNum,2000);//接收短消息
});
function resolveHash(){
	var h=window.location.hash;
	curhashpath="";
	if(h){
		curhashpath=h.replace("#!","");
	if(!curhashpath || curhashpath=='/'){
		gopage(ctx+"/platform/UI/default/main.jsp");
		$("#home_nav").html("<a href='javascript:;'>主页</a>");
	}
	else{
		if(curhashpath.indexOf("menu_")==0){
			var menuid=curhashpath.replace("menu_","");
			$w.doUrl(ctx+"/platform/menu/MenuPage@getMenuUrl.page",{menuId:menuid},function(data){
				if(data){
				if(data.opentype=='javascript'){
					eval(data.javascript);
				}else{
					if(data.url){
						var url="";
						if(data.url.indexOf("http://")==0)
							url=data.url;
						else
							url=ctx+data.url;
						//$w.closeCurrTab();
						var ops={title:data.name,href:url,id:'tab_'+menuid};
						if(data.icon)
							ops.iconUrl=ctx+data.icon;
						if(data.isRefresh!=0)
							ops.fresh=true;
						if(data.opentype=='open'){
							window.open(url);
						}else
							gopage(url);
						//var t=$('#main_Tabs').tabs('getSelected');
						var nav="";
						for(i=0;i<data.menuHome.length;i++){
							if(i==data.menuHome.length-1)
								nav+="<a href=\"javascript:main_left_click('"+data.menuHome[i].value+"','"+data.menuHome[i].text+"');\">"+data.menuHome[i].text+"</a>"
							else
								nav+="<a href='javascript:;'>"+data.menuHome[i].text+"</a>"
						}
						$("#home_nav").html(nav);
					}
					}}
				})
		}
	}
	}
}
//右键tab菜单事件
function tabRightEven() {
	//刷新
	$('#mm-tabupdate').click(function(){
		var currTab = $('#main_Tabs').tabs('getSelected');
		if(currTab.panel('options').href)
			currTab.panel('refresh');
		else{
			var iframe=$("iframe",currTab.panel("panel"));
			var url=iframe.attr('src');
			if(url){
				$('#main_Tabs').tabs('update', {
					tab: currTab,
					options:{
						content:'<iframe src="'+url+'" width="100%" height="100%" frameborder="0"></iframe>'
					}
				});
			}
			
		}
	})
	//关闭当前
	$('#mm-tabclose').click(function(){
		var currtab_title = $('#mm_tab').data("currtab");
		$('#main_Tabs').tabs('close',currtab_title);
	})
	//全部关闭
	$('#mm-tabcloseall').click(function(){
		$('.tabs-inner span').each(function(i,n){
			var t = $(n).text();
			if(i!=0) {
				$('#main_Tabs').tabs('close',t);
			}
		});
	});
	//关闭除当前之外的TAB
	$('#mm-tabcloseother').click(function(){
		var tabs=$("#main_Tabs").tabs("tabs");
		var tabsts=new Array();
		for(i=1;i<tabs.length;i++){
			tabsts[tabsts.length]=tabs[i].panel('options').title;
		}
		for(i=0;i<tabsts.length;i++){
			if(tabsts[i]!=$('#mm_tab').data("currtab"))
				$('#main_Tabs').tabs('close',tabsts[i]);
		}
		return false;
	});
}

function setMainLeftTitle(mname){
	$("#main_menus").panel("setTitle",mname);
}
function reloadMainLeft(url){
	$("#main_menus").panel("refresh",url);
}
var maxImmId=-1;
var lastId="";
var imm_receive=true;
var imm_err=0;
var maxBroadcastId=0;
function receiveImmNum(){//每25秒一次
	if(imm_err<10){//超过10次就不在检测了，超时了
		if(imm_receive){
		$w.doUrl(ctx+'/platform/message/imm/ImmPage@receiveImmNum.page',{},function(json){
			if(json.jsonpageinfo_ && json.jsonpageinfo_.stateCode=='300'){imm_err++;}
			if(json.maxid!=''){
				if(parseInt(json.maxid)>maxImmId){
					//有新的未提醒的消息，则发出声音提醒
					maxImmId=parseInt(json.maxid);
					lastId=json.minid;
					playSound("/platform/message/imm/default.wav");
					upimmicon("main_imm_iconsd",json.num);
					var imm=$("#main_imm",$("#main_menuIframe").contents().find("body"));
					imm.attr("title","您有新的消息");
					//if(window.frames["main_homeIframed"])
					//	window.frames["main_homeIframed"].main_reload();
				}else{
					var imm=$("#main_imm",$("#main_menuIframe").contents().find("body"));
					imm.attr("title","我的消息");
				}
			}
			//读取系统广播
			$w.doUrl(ctx+'/platform/message/imm/ImmPage@readBroadcast.page',{},function(json){
				if(json.data){
					$(json.data).each(function(){
						var mmstr=this.content+"&nbsp;("+this.immTime+")";
						if(parseInt(this.id)>maxImmId && lastId=='')lastId=this.id;
						if(this.soundFile!='')mmstr+="&nbsp;<a style='color:blue;cursor:pointer' onclick=\"playSound('"+this.soundFile+"')\">播放语音</a>"
					$.messager.show({
						title:"<span class='imm_broadcast'></span>系统广播",
						timeout:20000,
						msg:mmstr,
						width:300,
			  		height:150,
						showType:'show'
					});
					});
				}
			})
			window.setTimeout(receiveImmNum,20000);
		});
		}else
		window.setTimeout(receiveImmNum,20000);
	}
}
function playSound(snd){
	var snddiv=$("#imm_snd_div");
	snddiv.attr("src",ctx+snd+"?d="+new Date());
}
function upimmicon(cls,num){
	var immpa=document.body;
	if($("#main_menuIframe")[0])immpa=$("#main_menuIframe").contents().find("body");
	var imm=$("#main_imm_icon",immpa);
	imm.removeClass("main_imm_icon");
	imm.removeClass("main_imm_iconsd");
	imm.addClass(cls);
	if(cls=='main_imm_iconsd')
		$("#main_imm_txt",immpa).html("<b>我的消息("+num+")</b>");
	else
		$("#main_imm_txt",immpa).html("我的消息");
}
function open_immBox(){
	upimmicon("main_imm_icon");
	imm_receive=false; 
	$w.openWin({id:'mainImmBox_div',url:ctx+'/platform/message/imm/ImmPage.page?lastId='+lastId,iframe:true,w:700,h:480,title:'我的消息盒',collapsible:false,maximizable:false,icon:'icon_imm',afterClose:function(){
		imm_receive=true;
	}})
}
function close_immBox(){
	$("#mainImmBox_div").window("close");
}

function changeMyPwd(){
	$w.openWin({title:'修改我的密码',url:ctx+'/platform/user/UserManagePage@changeMyPwdShow.page?iswin=true',w:500,h:300})
}

function openMyTaskWork(){
	$w.addTab({title:'待办事务',href:ctx+'/platform/engine/work/WorkPage@showMyTask.page'});
}
function openMyUserInfo(){
	$w.openWin({title:'我的资料',url:ctx+'/contacts/ContactsPage@showInfo.page?edit=true&id='+$("[name='_userId_']").val(),w:600,h:460})
}

function changeSysStyle(s){
		$w.openWin({title:"切换系统界面风格",url:ctx+'/platform/UI/changeStyle.jsp?style='+s,w:550,h:440})
}
function _login_out(){
		window.location.href=ctx+"/platform/outlogin.jsp";
}
